<template>
  <div class="login">
    <div class="login-container">
      <!-- 头部 -->
      <div class="logo-header">
        <img
          class="logo"
          width="50"
          height="50"
          src="../../assets/imgs/logo.png"
          alt="logo"
        />
        <span class="title">系统登录</span>
      </div>
      <!-- 表单 -->
      <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm">
        <el-form-item prop="account">
          <el-input
            v-model="loginForm.account"
            prefix-icon="iconfont icon-zhanghu"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            :type="inputPasswordType"
            prefix-icon="iconfont icon-mima"
            v-model="loginForm.password"
            autocomplete="off"
            ><i
              slot="suffix"
              @click="changeType"
              class="iconfont"
              :class="{
                'icon-biyanjing': inputPasswordType === 'password',
                'icon-yanjing': inputPasswordType === 'text',
              }"
            ></i
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//引入工具函数
import { checkName, checkPwd } from "@/utils/verification.js";
import local from '../../utils/local'
// console.log(local,'kkk');

//引入ajax函数
import { checkLogin } from "../../api/user";
export default {
  //数据
  data() {
    return {
      //登录表单
      loginForm: {
        account: "",
        password: "",
      },
      //验证规则
      rules: {
        account: [{ validator: checkName, trigger: "blur" }],
        password: [{ validator: checkPwd, trigger: "blur" }],
      },
      //密码框类型
      inputPasswordType: "password",
    };
  },
  methods: {
    //登录触发
    submitForm(loginForm) {
      //触发表单所有的验证，全部通过。valid才是true，只要有一个不通过，就是false
      this.$refs[loginForm].validate(async (valid) => {
        //小貂蝉   a123456
        if (valid) {
          //发送登录ajax

          let res = await checkLogin(this.loginForm);
          console.log(valid, this.loginForm);
          console.log(res.data,'11');
          //取出后端返回的4个字段
          let { code, msg, role, token } = res.data;

          if (code === 0) {
            // window.localStorage.setItem('token',token)
            // console.log(local,22222)
            local.set('token',token)
            // this.$message({
            //   message: msg,
            //   type: "success",
            // });
            this.$router.push('/home')

            
          }else {
          // this.$message.error(msg);
          }
        }
      });
    },
    //切换密码框类型
    changeType() {
      if (this.inputPasswordType === "password") {
        this.inputPasswordType = "text";
      } else {
        this.inputPasswordType = "password";
      }
    },
  },
};
</script>

<style lang="less" scoped>
@keyframes turn {
  0% {
    -webkit-transform: rotate(0deg);
  }
  25% {
    -webkit-transform: rotate(90deg);
  }
  50% {
    -webkit-transform: rotate(180deg);
  }
  75% {
    -webkit-transform: rotate(270deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
.login {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-container {
    width: 400px;
    height: 300px;

    .logo-header {
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      margin-bottom: 20px;
      .logo {
        margin-right: 30px;
        animation: turn 1s linear infinite;
      }
      .title {
        font-weight: 500;
        font-size: 20px;
      }
    }
  }
  .el-button {
    width: 100%;
  }
  /* 深度选择器 */
  /deep/.el-input__inner {
    background: transparent;
    letter-spacing: 1px;
    color: #fff;
  }
}
</style>